

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.ico">
  <link rel="icon" href="/img/favicon.ico">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="Try not to become a man of success but rather try to become a man of value. ">
  <meta name="author" content="kinzun">
  <meta name="keywords" content>
  <meta name="description" content="初始面对对象 面对对象和面对过程   ⾯面向过程: ⼀一切以事物的流程为核心. 核心是”过程”⼆二字, 过程是指解决问题的步骤,即, 先干什么, 后干什么. 基于该思想编写程序就好比在编写⼀一套流⽔线. 是⼀一种机械式的编程思维  根据业务逻辑从上到下写垒代码   优点: 负责的问题流程化, 编写相对简单 缺点: 可扩展性差    ⾯面向对象:Object Oriented Programming">
<meta name="keywords" content="Python">
<meta property="og:type" content="article">
<meta property="og:title" content="Python:初始面对对象(1)">
<meta property="og:url" content="https://kinzun.github.io/2018/09/26/Python初始面对对象(1)/index.html">
<meta property="og:site_name" content="kinzun">
<meta property="og:description" content="初始面对对象 面对对象和面对过程   ⾯面向过程: ⼀一切以事物的流程为核心. 核心是”过程”⼆二字, 过程是指解决问题的步骤,即, 先干什么, 后干什么. 基于该思想编写程序就好比在编写⼀一套流⽔线. 是⼀一种机械式的编程思维  根据业务逻辑从上到下写垒代码   优点: 负责的问题流程化, 编写相对简单 缺点: 可扩展性差    ⾯面向对象:Object Oriented Programming">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2018-10-04T08:12:40.677Z">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="Python:初始面对对象(1)">
<meta name="twitter:description" content="初始面对对象 面对对象和面对过程   ⾯面向过程: ⼀一切以事物的流程为核心. 核心是”过程”⼆二字, 过程是指解决问题的步骤,即, 先干什么, 后干什么. 基于该思想编写程序就好比在编写⼀一套流⽔线. 是⼀一种机械式的编程思维  根据业务逻辑从上到下写垒代码   优点: 负责的问题流程化, 编写相对简单 缺点: 可扩展性差    ⾯面向对象:Object Oriented Programming">
  
  <title>Python:初始面对对象(1) - kinzun</title>

  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/css/bootstrap.min.css">


  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4/github-markdown.min.css">
  <link rel="stylesheet" href="/lib/hint/hint.min.css">

  
    
    
      
      <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10/styles/github-gist.min.css">
    
  

  
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.css">
  


<!-- 主题依赖的图标库，不要自行修改 -->
<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">

<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">

<link rel="stylesheet" href="/css/main.css">

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"kinzun.github.io","root":"/","version":"1.8.12","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname"}},"search_path":"/local-search.xml"};
  </script>
  <script src="/js/utils.js"></script>
  <script src="/js/color-schema.js"></script>
</head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>kinzun</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/img/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="Python:初始面对对象(1)">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2018-09-26 13:07" pubdate>
        2018年9月26日 下午
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      3.4k 字
    </span>
  

  
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      11 分钟
    </span>
  

  
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">Python:初始面对对象(1)</h1>
            
            <div class="markdown-body">
              <h4 id="初始面对对象"><a href="#初始面对对象" class="headerlink" title="初始面对对象"></a>初始面对对象</h4><ul>
<li><h5 id="面对对象和面对过程"><a href="#面对对象和面对过程" class="headerlink" title="面对对象和面对过程"></a>面对对象和面对过程</h5><ol>
<li><blockquote>
<p> ⾯面向过程: ⼀一切以事物的流程为核心. 核心是”过程”⼆二字, 过程是指解决问题的步骤,即, 先干什么, 后干什么. 基于该思想编写程序就好比在编写⼀一套流⽔线. 是⼀一种机械式的编程思维</p>
<p> 根据业务逻辑从上到下写垒代码</p>
</blockquote>
<ul>
<li>优点: 负责的问题流程化, 编写相对简单</li>
<li>缺点: 可扩展性差</li>
</ul>
</li>
<li><blockquote>
<p>⾯面向对象:Object Oriented Programming，OOP， ⼀一切以对象为中⼼心.<br>什么是对象? 不好解释. 先解释解释什么是车? 有轱辘, 有方向盘, 有发动机, 会跑的<br>是车. 好. 在解释⼀一个. 什么是人. 有名字, 年龄, 爱好, 会唱歌跳舞思考的是人. 我们给这两个东西下了了⼀一个简单的定义. 总结: 具有相同属性和动作的结合体叫对象. 面向对象思维, 要自己建立对象. 自己建立场景. 你是就是面向对象世界中的上帝. 你想让车干嘛就干嘛. 你想让人干嘛人就能干嘛.</p>
<p>对函数进行分类和封装，让开发“更快更好更强…”</p>
</blockquote>
<ul>
<li><strong>优点: 可扩展性强</strong></li>
<li>缺点: 编程的复杂度⾼高于⾯面向过程</li>
</ul>
</li>
</ol>
</li>
<li><p>类(<code>Class</code>)和实例(<code>Instance</code>)是面向对象最重要的概念。</p>
<blockquote>
<p>类与对象的关系: 类是对事物的总结. 抽象的概念. 类用来描述对象. 对象是类的实例<br>化的结果. 对象能执行哪些方法. 都由类来决定. 类中定义了什么. 对象就拥有什么</p>
</blockquote>
<ul>
<li>类和对象是面向对象中的两个重要概念</li>
</ul>
<ol>
<li>类：类有静态属性和静态方法，类无法访问动态属性和动态方法；</li>
<li>对象：对象有动态属性和动态方法，对象可以访问静态属性和静态方法；</li>
</ol>
<p>在python中定义一个类</p>
<figure class="highlight python"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Cars</span>:</span><br>    block<br></code></pre></div></td></tr></table></figure>
</li>
<li><p>现在我们来定义一辆车,把车特征信息写进类定义中,在python中 通过特殊的<code>__init__</code>方法实现.</p>
<figure class="highlight python"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs python"><span class="hljs-comment">#定义一辆车</span><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Car</span>:</span><br>    <span class="hljs-comment">#出厂设置(__int__) 构造方法</span><br>    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self,color,pai,pailinag,own)</span>:</span> <span class="hljs-comment">#表示当前类的对象</span><br>        <span class="hljs-comment">#print("造车")</span><br>        <span class="hljs-comment">#print(self)</span><br>        <span class="hljs-comment">#绑定属性.</span><br>        self.color1 =  color<br>        self.pai = pai<br>        self.pailing = pailinag<br>        self.own = own<br>    <span class="hljs-comment">#车能跑,跑就是一个动作,要写函数(方法)</span><br>    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">run</span><span class="hljs-params">(self)</span>:</span> <span class="hljs-comment">#self: 当前类的对象.</span><br>        print(<span class="hljs-string">"%s车飞起来了"</span>% self.color1)<br>        print(self)<br>    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">tiaogao</span><span class="hljs-params">(self)</span>:</span><br>        print(<span class="hljs-string">"有一台%s车在疯狂调高"</span>% self.color1)<br>         c1 = Car(<span class="hljs-string">"红色"</span>,<span class="hljs-string">"川B9BB95"</span>,<span class="hljs-string">"2.0L"</span>,<span class="hljs-string">'manotu'</span>) <span class="hljs-comment"># 自动调用int函数(方法) #c1是对Car()的实例化</span><br>c1.run() <span class="hljs-comment">#对象.方法</span><br><br>c1.tiaogao()<br><br>print(c1)<br><br>print(c1.dict)<br><br>print(c1.color1)<br><br>c1.color1= <span class="hljs-string">"绿色"</span><br><br>print(c1.color1)<br><br>  <span class="hljs-comment">#</span><br><br><span class="hljs-comment">#</span><br>c2 = Car(<span class="hljs-string">"宝蓝色"</span>,<span class="hljs-string">"川B66666"</span>,<span class="hljs-string">"3.0T"</span>,<span class="hljs-string">"baozi"</span>) <br><br>c2.run() <span class="hljs-comment">#执行run方法</span><br>print(c2)<br></code></pre></div></td></tr></table></figure>
<p> <em>ps：类中的函数第一个参数必须是<code>self</code> </em> </p>
</li>
</ul>
<p> 类中定义的函数叫做 <em>“方法”</em></p>
<h4 id="面对对象的三大特征"><a href="#面对对象的三大特征" class="headerlink" title="面对对象的三大特征"></a>面对对象的三大特征</h4><blockquote>
<p><strong>封装 </strong>  <strong>继承</strong> <strong>多态</strong></p>
</blockquote>
<h5 id="一-封装"><a href="#一-封装" class="headerlink" title="一.封装"></a>一.封装</h5><blockquote>
<p>封装: 把具有相同属性和功能的内容封装在一个对象中</p>
</blockquote>
<p>  在使用面向对象的封装特性时，需要：</p>
<ul>
<li><p>将内容封装到对象</p>
</li>
<li><p>从对象调用被封装的内容</p>
<ol>
<li>将内容封装到某处</li>
</ol>
<figure class="highlight python"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs python"><br><br><br><span class="hljs-comment">#创建类</span><br><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Foo</span>:</span><br>    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self,name,age)</span>:</span> <span class="hljs-comment">#称为构建方法,根据类创建对象时自动执行</span><br>        self.name = name<br>        self.age =age<br>        <br><span class="hljs-comment">#根据类Foo创建对象</span><br><span class="hljs-comment">#自动执行Foo类的__init__方法</span><br><span class="hljs-comment">#将amntou 和18 分别封装到obj1 self 的name 和age 属性中</span><br>obj1 = Foo(<span class="hljs-string">"amntou"</span>,<span class="hljs-number">18</span>)<br><br>obj2 = Foo(<span class="hljs-string">"baozi"</span>,<span class="hljs-number">20</span>)<br></code></pre></div></td></tr></table></figure>
</li>
</ul>
<ol start="2">
<li><p>调用被封装的内容时，有两种情况：</p>
<ul>
<li>通过对象直接调用</li>
<li>通过self间接调用</li>
</ul>
<ol>
<li>通过对象直接调用  <strong>对象.属性名 </strong></li>
</ol>
</li>
</ol>
<div class="hljs code-wrapper"><pre><code>   <figure class="highlight python"><table><tr><td class="gutter hljs"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Foo</span>:</span><br>    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self,name,age)</span>:</span> <br>        self.name = name<br>        self.age =age<br>obj1 = Foo(<span class="hljs-string">"amntou"</span>,<span class="hljs-number">18</span>)<br>print(obj1.name) <span class="hljs-comment">#直接调用obj1对象name属性</span><br>obj2 = Foo(<span class="hljs-string">"baozi"</span>,<span class="hljs-number">20</span>)<br>print(obj2.age) <span class="hljs-comment"># 直接调obj2对象 age属性</span><br></code></pre></div></td></tr></table></figure>


   ​

   ​

   ​

   2. 通过self间接调用被封装的内容

执行类中的方法时，需要通过self间接调用被封装的内容

<figure class="highlight python"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Foo</span>:</span><br> <br>    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, name, age)</span>:</span><br>        self.name = name<br>        self.age = age<br> <br>    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">detail</span><span class="hljs-params">(self)</span>:</span><br>        <span class="hljs-keyword">print</span> self.name<br>        <span class="hljs-keyword">print</span> self.age<br> <br>obj1 = Foo(<span class="hljs-string">'wupeiqi'</span>, <span class="hljs-number">18</span>)<br>obj1.detail()  <span class="hljs-comment"># Python默认会将obj1传给self参数，即：obj1.detail(obj1)，所以，此时方法内部的 self ＝ obj1，即：self.name 是 wupeiqi ；self.age 是 18</span><br> <br>obj2 = Foo(<span class="hljs-string">'alex'</span>, <span class="hljs-number">73</span>)<br>obj2.detail()  <span class="hljs-comment"># Python默认会将obj2传给self参数，即：obj1.detail(obj2)，所以，此时方法内部的 self ＝ obj2，即：self.name 是 alex ； self.age 是 78</span><br></code></pre></div></td></tr></table></figure>


&gt; ** 综上所述，对于面向对象的封装来说，其实就是使用构造方法将内容封装到 对象 中，然后通过对象直接或者self间接获取被封装的内容。 **
</code></pre><h5 id="二-继承"><a href="#二-继承" class="headerlink" title="二 继承"></a>二 继承</h5><blockquote>
<p>继承，面向对象中的继承和现实生活中的继承相同，即：子可以继承父的内容。</p>
</blockquote>
<p>  ​</p>
  <figure class="highlight python"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs python"><br><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Animal</span>:</span> <span class="hljs-comment">#在此代码段中,为父类</span><br>    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">eat</span><span class="hljs-params">(self)</span>:</span><br>        print(<span class="hljs-string">"%s吃"</span>% self.name)<br>    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">dirnk</span><span class="hljs-params">(self)</span>:</span><br>        print(<span class="hljs-string">"%s喝"</span>% self.name)<br>    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">sleep</span><span class="hljs-params">(self)</span>:</span><br>        print(<span class="hljs-string">"%s睡"</span>% self.name)<br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Cat</span><span class="hljs-params">(Animal)</span>:</span><span class="hljs-comment"># 在类名括号中写入另一个类名表示继承另一个类</span><br><br>    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self,name)</span>:</span><br>        self.name = name<br>        self.breed = <span class="hljs-string">"猫"</span><br>    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">cry</span><span class="hljs-params">(self)</span>:</span><br>        print(<span class="hljs-string">"喵喵叫"</span>)<br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Dog</span><span class="hljs-params">(Animal)</span>:</span> <span class="hljs-comment"># 在类名括号中写入另一个类名表示继承另一个类</span><br><br>    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self,name)</span>:</span><br>        self.name = name<br>        self.breed = <span class="hljs-string">"狗"</span><br>    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">cry</span><span class="hljs-params">(self)</span>:</span><br>        print(<span class="hljs-string">"汪汪"</span>)<br><br><br>cat = Cat(<span class="hljs-string">"福禄丸"</span>)<br>cat.eat()<br>cat.cry()<br>cat.dirnk()<br>dog = Dog(<span class="hljs-string">"帕克"</span>)<br>dog.cry()<br>dog.dirnk()<br></code></pre></div></td></tr></table></figure>
<blockquote>
<p><strong>所以，对于面向对象的继承来说，其实就是将多个类共有的方法提取到父类中，子类仅需继承父类而不必一一实现每个方法。</strong></p>
</blockquote>
  <figure class="highlight"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs python">class 父类<br>	def 父类中的方法(self): #子类继承父类,即拥有了父类中所以方法<br>     #<br>class 子类(父类):<br>  pass<br>zi = 子类() #创建子类对象<br>zi.父类中的方法() #执行从父类中继承的方法<br></code></pre></div></td></tr></table></figure>
<blockquote>
<ol>
<li><p>python的类可以继承多个类,java 和c#中则只能继承一个类</p>
</li>
<li><p>python的类继承了多累,寻找方法的方式为两种分别是<strong>深度优先</strong> 和 <strong>广度优先</strong></p>
<ul>
<li><strong>如果是经典类MRO为DFS（深度优先搜索（子节点顺序：从左到右）</strong></li>
</ul>
</li>
</ol>
<ul>
<li><strong>如果是新式类MRO为BFS（广度优先搜索（子节点顺序：从左到右））</strong></li>
<li>Python 至少有两种不同的 MRO：<ul>
<li>经典类（classic class）的深度遍历。</li>
<li>Python 2.2 的新式类 MRO</li>
<li>Python 2.3 的新式类的C3 算法。它也是 Python 3 唯一支持的方式。</li>
</ul>
</li>
</ul>
</blockquote>
<p>  <a href="http://xymlife.com/2016/05/22/python_mro/" target="_blank" rel="noopener">mor算法参考博客</a></p>
  <figure class="highlight python"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs python"><span class="hljs-comment">#经典类</span><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">C1</span>:</span><br>	<span class="hljs-keyword">pass</span><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">C2</span><span class="hljs-params">(C1)</span>:</span>  C2是经典类<br>  <span class="hljs-keyword">pass</span><br><br><br><span class="hljs-comment"># 新式类</span><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">N1</span><span class="hljs-params">(object)</span>:</span><br>  <span class="hljs-keyword">pass</span><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">N1</span><span class="hljs-params">(N1)</span>:</span><br>  <span class="hljs-keyword">pass</span><br></code></pre></div></td></tr></table></figure>
<h4 id="三-多态"><a href="#三-多态" class="headerlink" title="三 多态"></a>三 多态</h4>
            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/Python/">Python</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2018/09/26/Python面对对象成员(2)/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Python:面对对象成员(2)</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2018/09/26/python-base速写/">
                        <span class="hidden-mobile">Python base 速写</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
              <!-- Comments -->
              <article class="comments" id="comments" lazyload>
                
                  
                
                

              </article>
            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
  </div>
  

  

  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  <script  src="/js/local-search.js" ></script>



  
    <script  src="/js/img-lazyload.js" ></script>
  



  



  
    <script  src="https://cdn.jsdelivr.net/npm/tocbot@4/dist/tocbot.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4/anchor.min.js" ></script>
  
  
    <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js" ></script>
  






  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
      typing(title)
      
    })(window, document);
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>


</body>
</html>
